package cn.stronglink.assetmanage.business.search.presenter;

import android.content.Context;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.greendao.query.QueryBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.business.search.view.ISearchView;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.dao.SyncTypesDao;
import cn.stronglink.assetmanage.entity.Result;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.entity.Status;
import cn.stronglink.assetmanage.entity.SyncTypes;
import cn.stronglink.assetmanage.entity.Type;
import cn.stronglink.assetmanage.utils.ImageUtils;
import cn.stronglink.netrequest.business.base.BaseDataService;
import cn.stronglink.netrequest.business.search.SearchService;
import cn.stronglink.netrequest.exceptions.BusinessErrorException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SearchListPresenter {
    private ISearchView searchView;
    private SearchService searchService;
    private Gson gson;
    private Context mContext;
    private Handler mHandler;
    private BaseDataService baseDataService;

    public SearchListPresenter(Context context, ISearchView searchView) {
        this.mContext = context;
        this.searchView = searchView;
        searchService = new SearchService();
        baseDataService = new BaseDataService();
        gson = new Gson();
        mHandler = new Handler();
    }

    public void queryAssetListFromLocal() {
        List<SearchAsset> searchAssetList = DaoManager.getDaoSession(mContext).getSearchAssetDao().queryBuilder().
                orderDesc(SearchAssetDao.Properties.UpdateTime).list();
        if (searchAssetList != null) {
            searchView.onSuccess(searchAssetList);
        }
    }


    // 保存数据到本地
    public void saveDataToLocal(List<SearchAsset> searchAssetList) {
        if (searchAssetList != null) {
            DaoManager.getDaoSession(mContext).getSearchAssetDao().deleteAll();
            for (SearchAsset asset : searchAssetList) {
                if (asset.getImgPath() != null && !asset.getImgPath().isEmpty()) {
                    String url = ImageUtils.base64ToFile(asset.getImgPath());
                    if (url != null) {
                        asset.setPictureUri(url);
                    }
                }
                DaoManager.getDaoSession(mContext).getSearchAssetDao().insertOrReplace(asset);
            }
        }
    }


    // 查询通过扫码
    public void queryByScan(String value) {
        // 查本地数据
        List<SearchAsset> searchAssetList = DaoManager.getDaoSession(mContext).
                getSearchAssetDao().queryBuilder().whereOr(
                SearchAssetDao.Properties.SerialNo.eq(value),
                SearchAssetDao.Properties.RfidNo.eq(value),
                SearchAssetDao.Properties.AssetNo.eq(value)).
                orderDesc(SearchAssetDao.Properties.UpdateTime).list();
        if (searchAssetList != null && searchAssetList.size() > 0) {
            searchView.onSuccess(searchAssetList);
        } else {
            searchView.scanDataIsNotInAsset();
        }
    }

    public void queryByInput() {
        //只查离线数据
        QueryBuilder<SearchAsset> qb = DaoManager.getDaoSession(mContext).getSearchAssetDao().queryBuilder();
        if (searchView.getAssetNameText() != null && !searchView.getAssetNameText().isEmpty()) {
            qb.whereOr(SearchAssetDao.Properties.AssetNo.like("%" + searchView.getAssetNameText() + "%"),
                    SearchAssetDao.Properties.RfidNo.like("%" + searchView.getAssetNameText() + "%"),
                    SearchAssetDao.Properties.EndUser.like("%" + searchView.getAssetNameText() + "%"));
        }
        String departmentName = searchView.getDepartment();
        if (departmentName != null && !departmentName.isEmpty() && !"请选择部门".equals(departmentName)) {
            qb.where(SearchAssetDao.Properties.DatacenterId_.eq(searchView.getDepartment()));
        }
        if (searchView.getAssetRoom() != null && !searchView.getAssetRoom().isEmpty()) {
            qb.where(SearchAssetDao.Properties.RoomId_.eq(searchView.getAssetRoom()));
        }
        if (searchView.getAssetStatus() != 0) {
            qb.where(SearchAssetDao.Properties.Status.eq(searchView.getAssetStatus()));
        }
        if (searchView.getAssetType() != null && !searchView.getAssetType().isEmpty()) {
            qb.where(SearchAssetDao.Properties.TypeId_.eq(searchView.getAssetType()));
        }
        List<SearchAsset> searchAssetList = qb.orderDesc(SearchAssetDao.Properties.UpdateTime).list();
        if (searchAssetList != null) {
            searchView.onSuccess(searchAssetList);
        }
    }

    /**
     * Spinner 资产类型
     *
     * @return
     */
    public List<Type> queryTypeList() {
        List<Type> typeList = new ArrayList<>();
        Type type = new Type();
        type.setId_("");
        type.setName("请选择类型");
        typeList.add(type);

        List<Type> types = DaoManager.getDaoSession(mContext).getTypeDao().loadAll();
        if (types != null && types.size() > 0) {
            typeList.addAll(types);
        }
        return typeList;
    }

    /**
     * Spinner 资产状态
     *
     * @return
     */
    public List<Status> queryStatusList() {
        List<Status> statusList = new ArrayList<>();
        final Status status = new Status();
        status.setId(0);
        status.setName("请选择状态");
        statusList.add(status);
        Status status1 = new Status();
        status1.setId(1);
        status1.setName("在库");
        statusList.add(status1);
        Status status2 = new Status();
        status2.setId(2);
        status2.setName("已上架");
        statusList.add(status2);
        Status status3 = new Status();
        status3.setId(3);
        status3.setName("已报废");
        statusList.add(status3);
        Status status5 = new Status();
        status5.setId(5);
        status5.setName("待验收");
        statusList.add(status5);
        Status status6 = new Status();
        status6.setId(6);
        status6.setName("使用中");
        statusList.add(status6);
        return statusList;
    }

    /**
     * Spinner 地点
     *
     * @return
     */
    public List<Room> queryRoomList() {
        List<Room> roomList = new ArrayList<>();
        Room room = new Room();
        room.setId_("");
        room.setName("请选择地点");
        roomList.add(room);
        List<Room> rooms = DaoManager.getDaoSession(mContext).getRoomDao().loadAll();
        if (rooms != null && rooms.size() > 0) {
            roomList.addAll(rooms);
        }
        return roomList;
    }

    public List<SearchAsset> querySearchAssetList() {
        List<SearchAsset> searchAssetList = new ArrayList<>();
        searchAssetList = DaoManager.getDaoSession(mContext).getSearchAssetDao().queryBuilder().
                orderDesc(SearchAssetDao.Properties.UpdateTime).list();
        return searchAssetList;
    }
}
